﻿// -----------------------------------------------------------------------
// <copyright file="Department_DAL.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace PMSGSC.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Department_DAL:BaseDAL
    {
        // Lấy tất cả Department
        public static List<Department> GetAll(int shipID)
        {
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.Departments);
            return DB.Departments.Where(n => n.Ship_ID == shipID).ToList();
        }
        // Lấy Department theo Id
        public static Department GetDataById(int id)
        {
            return DB.Departments.Where(m => m.Department_ID == id).FirstOrDefault();
        }
        public static List<Department> GetDataListByName(int id, int shipID)
        {
            return DB.Departments.Where(n => (n.Department_ID == id) && (n.Ship_ID == shipID)).ToList();
        }
        //Thêm một Department
        public static bool Insert(Department Department)
        {
            try
            {
                DB.Departments.InsertOnSubmit(Department);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Xóa một Department
        public static bool Delete(Department Department)
        {
            try
            {
                DB.Departments.DeleteOnSubmit(Department);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Cập nhật Department
        public static bool Update(Department Department)
        {
            try
            {
                //Department objDepartment = GetDataById(Department.Department_ID);
                //objDepartment.CodeDepartment = Department.CodeDepartment;
                //objDepartment.NameDepartment = Department.NameDepartment;
                //objDepartment.Address = Department.Address;
                //objDepartment.Tell = Department.Tell;
                //objDepartment.Email = Department.Email;
                //objDepartment.ContactPerson = Department.ContactPerson;
                //objDepartment.Fax = Department.Fax;
                //objDepartment.Note = Department.Note;
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }

        // Kiểm tra xem Department Code có tồn tại chưa ?
        public static bool IsExits(Department Department, int shipID)
        {
            Department objDepartment = null;
            objDepartment = DB.Departments.Where(m => (m.DepartmentName == Department.DepartmentName)
                && (m.Department_ID != Department.Department_ID) && (m.Ship_ID == shipID)).FirstOrDefault();
            return objDepartment != null ? true : false;
        }
        public static bool IsRelationship(Department department)
        {
            Employee employee = DB.Employees.Where(n => n.Department_ID == department.Department_ID).FirstOrDefault();
            if (null != employee)
                return false;

            return true;
        }

    }
}
